package com.zxl.live.tools.tools;


import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.ActivityManager;
import android.bluetooth.BluetoothAdapter;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.PowerManager;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.WindowManager;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.List;
import java.util.Locale;

import static android.os.Build.VERSION_CODES.HONEYCOMB;
import static android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH;
import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
import static android.os.Build.VERSION_CODES.KITKAT;

public class MachineUtils {

    private static int sScreenWidth = 0;
    private static int sScreenHeight = 0;

    // SDK 版本判断
    public static final int SDK_VERSION = Build.VERSION.SDK_INT;

    /**
     * SDK >= 18
     */
    public static final boolean HAS_SDK_LOLLIPOP = SDK_VERSION >= Build.VERSION_CODES.LOLLIPOP;

    /**
     * SDK >= 18
     */
    public static final boolean HAS_SDK_JELLY_BEAN_MR2 = SDK_VERSION >= JELLY_BEAN_MR2;

    public static final boolean HAS_SDK_JELLY_BEAN = SDK_VERSION >= Build.VERSION_CODES.JELLY_BEAN;

    /**
     * SDK >= 19
     */
    public static final boolean HAS_SDK_KITKAT = SDK_VERSION >= KITKAT;

    /**
     * SDK >= 14
     */
    public static final boolean HAS_SDK_ICS = SDK_VERSION >= ICE_CREAM_SANDWICH;
    /**
     * SDK >= 11
     */
    public static final boolean HAS_SDK_HONEYCOMB = SDK_VERSION >= HONEYCOMB;

    /**
     * 是否屏亮
     *
     * @param context
     * @return
     */
    public static boolean isScreenOn(Context context) {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        return pm.isScreenOn();
    }

    /**
     * 获取当前的语言
     *
     * @param context
     * @return
     * @author zhoujun
     */
    public static String getLanguage(Context context) {
        String language = Locale.getDefault().getLanguage().toLowerCase();
        // 标准返回是2位
        if (language.length() != 2) {
            language = context.getResources().getConfiguration().locale.getLanguage().toLowerCase();
        }
        if (language.length() > 2) {
            language = language.substring(0, 2);
        }
        return language;
    }

    /**
     * 获取当前的国家
     *
     * @param context
     * @return
     * @author zhoujun
     */
    public static String getCountry(Context context) {
        String country = Locale.getDefault().getCountry();
        // 标准返回是2位
        if (country.length() != 2) {
            country = context.getResources().getConfiguration().locale.getCountry().toUpperCase();
        }
        if (country.length() > 2) {
            country = country.substring(0, 2);
        }
        return country;
    }

    /**
     * 获取当前的网络类型
     *
     * @param context
     * @return
     */
    public static String getNetWorkType(Context context) {
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        String netkWorkType = "unknown";
        if (networkInfo != null && networkInfo.isConnected()) {
            String type = networkInfo.getTypeName();
            if (type.equalsIgnoreCase("WIFI")) {
                netkWorkType = "wifi";
            } else if (type.equalsIgnoreCase("MOBILE")) {
                TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                switch (telephonyManager.getNetworkType()) {
                    case TelephonyManager.NETWORK_TYPE_1xRTT:
                        netkWorkType = "2g";
                        break;
                    case TelephonyManager.NETWORK_TYPE_CDMA:
                        netkWorkType = "2g";
                        break;
                    case TelephonyManager.NETWORK_TYPE_EDGE:
                        netkWorkType = "2g";
                        break;
                    case TelephonyManager.NETWORK_TYPE_EVDO_0:
                        netkWorkType = "3g";
                        break;
                    case TelephonyManager.NETWORK_TYPE_EVDO_A:
                        netkWorkType = "3g";
                        break;
                    case TelephonyManager.NETWORK_TYPE_GPRS:
                        netkWorkType = "gprs";
                        break;
                    case TelephonyManager.NETWORK_TYPE_HSDPA:
                        netkWorkType = "3g";
                        break;
                    case TelephonyManager.NETWORK_TYPE_HSPA:
                        netkWorkType = "3g";
                        break;
                    case TelephonyManager.NETWORK_TYPE_HSUPA:
                        netkWorkType = "3g";
                        break;
                    case TelephonyManager.NETWORK_TYPE_UMTS:
                        netkWorkType = "3g";
                        break;
                    case TelephonyManager.NETWORK_TYPE_EHRPD:
                        netkWorkType = "3g";
                        break;
                    case TelephonyManager.NETWORK_TYPE_EVDO_B:
                        netkWorkType = "3g";
                        break;
                    case TelephonyManager.NETWORK_TYPE_HSPAP:
                        netkWorkType = "3g";
                        break;
                    case TelephonyManager.NETWORK_TYPE_IDEN:
                        netkWorkType = "2g";
                        break;
                    case TelephonyManager.NETWORK_TYPE_LTE:
                        netkWorkType = "4g";
                        break;
                    case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                        netkWorkType = "2g";
                        break;
                    default:
                        netkWorkType = "unknown";
                        break;
                }
            }
        }

        return netkWorkType;
    }

    /**
     * 获取当前进程名
     *
     * @param context
     * @return
     */
    public static String getCurrProcessName(Context context) {
        try {
            final int currProcessId = android.os.Process.myPid();
            final ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
            if (processInfos != null) {
                for (ActivityManager.RunningAppProcessInfo info : processInfos) {
                    if (info.pid == currProcessId) {
                        return info.processName;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    //sim卡是否可读
    public static boolean isCanUseSim(Context context) {
        try {
            TelephonyManager mgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            return TelephonyManager.SIM_STATE_READY == mgr.getSimState();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    /**
     * 获取gmail
     *
     * @param context
     * @return
     */
    public static String getGmail(Context context) {
        AccountManager accountManager = AccountManager.get(context);
        Account[] accounts = accountManager.getAccountsByType("com.google"); //获取google账户
        Account account = accounts.length > 0 ? accounts[0] : null; //取第一个账户
        return account == null ? null : account.name;
    }

    /**
     * 展开通知栏
     *
     * @param context
     */
    @SuppressWarnings("ResourceType")
    public static void expandNotificationsPanel(Context context) {
        try {
            Object statusbar = context.getSystemService("statusbar");
            Class statusBarManager = Class.forName("android.app.StatusBarManager");
            Method expandMethod = statusBarManager.getMethod("expandNotificationsPanel");
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {        //4.2以下
                expandMethod = statusBarManager.getMethod("expand");
            }
            expandMethod.invoke(statusbar);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 合上通知栏
     *
     * @param context
     */
    @SuppressWarnings("ResourceType")
    public static void collapsePanels(Context context) {
        try {
            Object statusbar = context.getSystemService("statusbar");
            Class statusBarManager = Class.forName("android.app.StatusBarManager");
            Method collapseMethod = statusBarManager.getMethod("collapsePanels");
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {        //4.2以下
                collapseMethod = statusBarManager.getMethod("collapse");
            }
            collapseMethod.invoke(statusbar);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 隐藏应用程序图标
     *
     * @param context
     * @return
     */
    public static boolean hideLauncherIcon(Context context) {
        ComponentName component = null;
        PackageManager packageManager = context.getPackageManager();
        Intent intent = packageManager.getLaunchIntentForPackage(context.getPackageName());
        if (intent != null) {
            component = intent.getComponent();
        }

        if (null == component) {
            if (null == intent) {
                intent = new Intent(Intent.ACTION_MAIN);
                intent.setPackage(context.getPackageName());
                intent.addCategory(Intent.CATEGORY_LAUNCHER);
            }
            List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, 0);
            if (null != resolveInfos && !resolveInfos.isEmpty()) {
                ResolveInfo resolveInfo = resolveInfos.get(0);
                if (null != resolveInfo && resolveInfo.activityInfo != null) {
                    component = new ComponentName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
                }
            }
        }


        if (null != component) {
            packageManager.setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
            return true;
        }
        return false;
    }

    public static boolean showLauncherIcon(Context context, Class<?> defaultActivity) {
        ComponentName component = null;
        PackageManager packageManager = context.getPackageManager();
        Intent intent = packageManager.getLaunchIntentForPackage(context.getPackageName());
        if (intent != null) {
            component = intent.getComponent();
        }

        if (null == component) {
            if (null == intent) {
                intent = new Intent(Intent.ACTION_MAIN);
                intent.setPackage(context.getPackageName());
                intent.addCategory(Intent.CATEGORY_LAUNCHER);
            }
            List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, 0);
            if (null != resolveInfos && !resolveInfos.isEmpty()) {
                ResolveInfo resolveInfo = resolveInfos.get(0);
                if (null != resolveInfo && resolveInfo.activityInfo != null) {
                    component = new ComponentName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
                }
            }
        }

        if (null == component) {
            component = new ComponentName(context, defaultActivity);
            Log.e("august", "can not find component, set it as StartActivity");
        }

        if (null != component) {
            packageManager.setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
            return true;
        }
        return false;
    }

    public static boolean isApkDebugable(Context context) {
        try {
            ApplicationInfo info = context.getApplicationInfo();
            return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    // 获得总内存
    public static long getTotalMem() {
        long mTotal;
        // /proc/meminfo读出的内核信息进行解释
        String path = "/proc/meminfo";
        String content = null;
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(path), 8);
            String line;
            if ((line = br.readLine()) != null) {
                content = line;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        // beginIndex
        int begin = content.indexOf(':');
        // endIndex
        int end = content.indexOf('k');
        // 截取字符串信息

        content = content.substring(begin + 1, end).trim();
        mTotal = Integer.parseInt(content);
        return mTotal;
    }

    // 获得可用的内存
    public static long getAvailableMemory(Context mContext) {
        long usedMemory;
        ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
        am.getMemoryInfo(mi);

        // 取得剩余的内存空间
        usedMemory = mi.availMem / 1024;
        return usedMemory;
    }

    // 获得总内存
    public static long getTotalMem(Context context) {
        long mTotal = 0;
        // /proc/meminfo读出的内核信息进行解释
        String path = "/proc/meminfo";
        String content = null;
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(path), 8);
            String line;
            if ((line = br.readLine()) != null) {
                content = line;
            }
            // beginIndex
            int begin = content.indexOf(':');
            // endIndex
            int end = content.indexOf('k');
            // 截取字符串信息

            content = content.substring(begin + 1, end).trim();
            mTotal = Integer.parseInt(content);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            if (0 == mTotal) {
                mTotal = 2 * getAvailableMemory(context);
            }
            return mTotal;
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return mTotal;
    }

    public static int getStatusBarH(Context context) {
        int sStatusBarHeight = 0;
        Class<?> c = null;
        Object obj = null;
        Field field = null;
        int x = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            sStatusBarHeight = context.getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return sStatusBarHeight;
    }

    public static int getScreenWidth(Context context) {
        if (sScreenWidth == 0) {
            DisplayMetrics dm = new DisplayMetrics();
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            wm.getDefaultDisplay().getMetrics(dm);
            sScreenWidth = dm.widthPixels;
        }
        return sScreenWidth;
    }

    public static int getScreenHeight(Context context) {
        if (sScreenHeight == 0) {
            DisplayMetrics dm = new DisplayMetrics();
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            wm.getDefaultDisplay().getMetrics(dm);
            sScreenHeight = dm.heightPixels;
        }
        return sScreenHeight;
    }

    public static int getBatteryLevel(Context context) {
        int batteryLevel = 0;
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent intentBattery = context.registerReceiver(null, intentFilter);        //注意，粘性广播不需要广播接收器
        if (intentBattery != null) {
            batteryLevel = intentBattery.getIntExtra("level", 0);
        }
        return batteryLevel;
    }

    /**
     * 是否是三星的手机
     *
     * @return
     */
    public static boolean isSamsungDevice() {
        return Build.BRAND != null &&
                Build.MANUFACTURER != null ?
                Build.BRAND.compareToIgnoreCase("Samsung") == 0 || Build.MANUFACTURER.compareToIgnoreCase("Samsung") == 0
                : false;
    }

    public static String getSignature(Context context, String pkgName) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);
            byte[] bs = packageInfo.signatures[0].toByteArray();

            CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
            X509Certificate cert = (X509Certificate) certFactory.generateCertificate(new ByteArrayInputStream(bs));
            String key = cert.getPublicKey().toString();
            return key.substring(key.indexOf("modulus: ") + 9, key.indexOf("\n", key.indexOf("modulus:")));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static boolean isWifiEnable(Context context) {
        try {
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            switch (wifiManager.getWifiState()) {
                case WifiManager.WIFI_STATE_ENABLED:
                    return true;
                case WifiManager.WIFI_STATE_DISABLED:
                    return false;
            }
            return wifiManager.isWifiEnabled();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static final String WIFI_STATE_CHANGED_ACTION = "action.wifi_state_changed";

    public static void setWifiEnable(final Context context, final boolean enable) {
        try {
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            boolean isSuccess = wifiManager.setWifiEnabled(enable);
            if (isSuccess) {
                new Thread() {

                    @Override
                    public void run() {
                        try {
                            sleep(200);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        try {
                            if (enable != isWifiEnable(context)) {
                                context.sendBroadcast(new Intent(WIFI_STATE_CHANGED_ACTION));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
                LogUtils.d("setWifiEnable :" + enable);
            } else {
                LogUtils.d("setWifiEnable失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取移动数据状态
     *
     * @param mContext
     * @return
     */
    public static boolean getMobileDataEnabled(Context mContext) {
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
            Method getMobileDataEnabledMethod = ConnectivityManager.class.getMethod("getMobileDataEnabled");
            return (Boolean) getMobileDataEnabledMethod.invoke(connectivityManager);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 设置移动数据状态
     *
     * @param mContext
     * @param enable
     */
    public static void setMobileDataEnabled(Context mContext, boolean enable) {
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
            Method setMobileDataEnabled = ConnectivityManager.class.getMethod("setMobileDataEnabled", boolean.class);
            setMobileDataEnabled.invoke(connectivityManager, enable);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void setBluetoothEnabled(boolean mIsOpenBluetooth) {
        try {
            if (mIsOpenBluetooth) {
                BluetoothAdapter.getDefaultAdapter().enable();
            } else {
                BluetoothAdapter.getDefaultAdapter().disable();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean getBluetoothEnabled() {
        try {
            return BluetoothAdapter.getDefaultAdapter() != null ? BluetoothAdapter.getDefaultAdapter().isEnabled() : false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static int getBluetoothState() {
        return BluetoothAdapter.getDefaultAdapter() != null ? BluetoothAdapter.getDefaultAdapter().getState() : 1;
    }

    public static boolean IS_JELLY_BEAN = Build.VERSION.SDK_INT >= 16;

    public static boolean isMIUI() {
        boolean result = false;
        String host = Build.HOST;
        if (IS_JELLY_BEAN && host != null && host.toLowerCase() != null && host.toLowerCase().contains("miui")) {
            result = true;
        }
        return result;
    }

    public static String getMiuiVer() {
        String ver = getSystemProperty("ro.miui.ui.version.name");
        return ver;
    }

    public static String getSystemProperty(String propName) {
        String line;
        BufferedReader input = null;
        try {
            Process p = Runtime.getRuntime().exec("getprop " + propName);
            input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
            line = input.readLine();
            input.close();
        } catch (IOException ex) {
            return null;
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                }
            }
        }
        return line;
    }

    public static boolean isMeizu() {
        return Build.BRAND.toLowerCase().contains("meizu") && Build.VERSION.SDK_INT >= 14;
    }

    public static boolean isHuawei() {
        boolean result = false;
        String androidVersion = Build.VERSION.RELEASE;// os版本号
        String brand = Build.BRAND;// 商标
        if (androidVersion == null || brand == null) {
            return result;
        }
        if (brand.equalsIgnoreCase("Huawei")) {
            result = true;
        }
        return result;
    }

    public static boolean isMiui() {
        return Build.MANUFACTURER.toLowerCase().contains("xiaomi");
    }

    public static boolean hasSimCard(Context context) {
        try {
            TelephonyManager telMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            int simState = telMgr.getSimState();
            boolean result = true;
            switch (simState) {
                case TelephonyManager.SIM_STATE_ABSENT:
                    result = false; // 没有SIM卡
                    break;
                case TelephonyManager.SIM_STATE_UNKNOWN:
                    result = false;
                    break;
            }
            return result;
        } catch (Throwable var3) {
            var3.printStackTrace();
        }
        return false;
    }

    private static float sUnitDip = 0;

    public static float getPxFromDp(Context context, int dp) {
        if (sUnitDip == 0) {
            sUnitDip = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, context.getResources().getDisplayMetrics());
        }
        return sUnitDip * dp;
    }
}
